#include "stdafx.h"
#include "MyControls/SegmentSetCmdImpl.h"
#include "MyControls/SegmentSet.h"
#include "MyControls/Sequence.h"
#include "MyControls/Resource.h"

namespace My {

BEGIN_MESSAGE_MAP(SegmentSetCmdImpl, CCmdTarget)
	ON_COMMAND(ID_TIME_SEGMENTBACKEND, OnTimeToSegmentBackEnd)
	ON_UPDATE_COMMAND_UI(ID_TIME_SEGMENTBACKEND, OnUpdateTimeToSegmentBackEnd)
	ON_COMMAND(ID_TIME_SEGMENTFRONTEND, OnTimeToSegmentFrontEnd)
	ON_UPDATE_COMMAND_UI(ID_TIME_SEGMENTFRONTEND, OnUpdateTimeToSegmentFrontEnd)

	ON_COMMAND(ID_SEGMENT_ACTIVATE_ABOVE, OnSegmentActivateAbove)
	ON_UPDATE_COMMAND_UI(ID_SEGMENT_ACTIVATE_ABOVE, OnUpdateSegmentActivateAbove)
	ON_COMMAND(ID_SEGMENT_ACTIVATE_BELOW, OnSegmentActivateBelow)
	ON_UPDATE_COMMAND_UI(ID_SEGMENT_ACTIVATE_BELOW, OnUpdateSegmentActivateBelow)
	ON_COMMAND(ID_SEGMENT_DEACTIVATE, OnSegmentDeactivate)
	ON_UPDATE_COMMAND_UI(ID_SEGMENT_DEACTIVATE, OnUpdateSegmentDeactivate)

	ON_COMMAND(ID_DISPLAY_EDITMASK, OnOpenMaskEditor)
	ON_UPDATE_COMMAND_UI(ID_DISPLAY_EDITMASK, OnUpdateOpenMaskEditor)
	ON_COMMAND(ID_DISPLAY_PREVIEW, OnCloseMaskEditor)
	ON_UPDATE_COMMAND_UI(ID_DISPLAY_PREVIEW, OnUpdateCloseMaskEditor)
END_MESSAGE_MAP()

//////////////////////////////
// Go to Segment Back/Front //
//////////////////////////////

void SegmentSetCmdImpl::OnTimeToSegmentBackEnd() 
{
	GetSequence()->SetCurrentFrame(
		GetSegmentSet()->GetActiveSegmentPtr()->GetTimeSpan().first);
}

void SegmentSetCmdImpl::OnUpdateTimeToSegmentBackEnd(CCmdUI* pCmdUI) 
{
	const Segment * s = GetSegmentSet()->GetActiveSegmentPtr();
	pCmdUI->Enable(s != 0 && s->GetTimeSpan().first != GetSequence()->GetCurrentFrame());
}

void SegmentSetCmdImpl::OnTimeToSegmentFrontEnd() 
{
	GetSequence()->SetCurrentFrame(
		GetSegmentSet()->GetActiveSegmentPtr()->GetTimeSpan().last - 1);
}

void SegmentSetCmdImpl::OnUpdateTimeToSegmentFrontEnd(CCmdUI* pCmdUI) 
{
	const Segment * s = GetSegmentSet()->GetActiveSegmentPtr();
	pCmdUI->Enable(s != 0 && s->GetTimeSpan().last-1 != GetSequence()->GetCurrentFrame());
}

///////////////////////////
// Change active segment //
///////////////////////////

void SegmentSetCmdImpl::OnSegmentActivateAbove()
{
	int n = GetSegmentSet()->GetActiveSegment();
	if (n == -1)
		n = GetSegmentSet()->GetSegmentCount() - 1;
	else
		--n; //0 becomes -1, it's ok
	GetSegmentSet()->SetActiveSegment(n);
}

void SegmentSetCmdImpl::OnUpdateSegmentActivateAbove(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(GetSegmentSet()->GetSegmentCount() > 0);
}

void SegmentSetCmdImpl::OnSegmentActivateBelow()
{
	int n = GetSegmentSet()->GetActiveSegment();
	if (n == GetSegmentSet()->GetSegmentCount() - 1)
		n = -1;
	else
		++n; //-1 becomes 0, it's ok
	GetSegmentSet()->SetActiveSegment(n);
}

void SegmentSetCmdImpl::OnUpdateSegmentActivateBelow(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(GetSegmentSet()->GetSegmentCount() > 0);
}

void SegmentSetCmdImpl::OnSegmentDeactivate()
{
	GetSegmentSet()->SetActiveSegment(-1);
}

void SegmentSetCmdImpl::OnUpdateSegmentDeactivate(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(GetSegmentSet()->GetActiveSegment() >= 0);
}

void SegmentSetCmdImpl::OnOpenMaskEditor()
{
	GetSegmentSet()->GetActiveSegmentPtr()->OpenMaskEditor();
}

void SegmentSetCmdImpl::OnUpdateOpenMaskEditor(CCmdUI *pCmdUI)
{
	Segment * pSeg = GetSegmentSet()->GetActiveSegmentPtr();
	pCmdUI->Enable(pSeg != 0);
	pCmdUI->SetRadio(pSeg != 0 && pSeg->IsMaskEditing());
}

void SegmentSetCmdImpl::OnCloseMaskEditor()
{
	GetSegmentSet()->GetActiveSegmentPtr()->CloseMaskEditor();
}

void SegmentSetCmdImpl::OnUpdateCloseMaskEditor(CCmdUI *pCmdUI)
{
	Segment * pSeg = GetSegmentSet()->GetActiveSegmentPtr();
	pCmdUI->Enable(pSeg != 0);
	pCmdUI->SetRadio(pSeg != 0 && !pSeg->IsMaskEditing());
}

} //namespace My
